package cn.leafcraft.utils.webutil.lang;

import java.util.Optional;

public final class CastUtils {

	@SuppressWarnings("unchecked")
	public static <T> T cast(Object obj) {
		return (T) obj;
	}

	public static <S, T> T checkedCast(S candidate, Class<T> targetClass) {
		if (targetClass.isInstance(candidate)) {
			return cast(candidate);
		}

		return null;
	}

	public static <S, T> Optional<T> optionalCast(S candidate, Class<T> targetClass) {
		return targetClass.isInstance(candidate)
				? Optional.of(targetClass.cast(candidate))
				: Optional.empty();
	}

	private CastUtils() {
        throw new UnsupportedOperationException();
	}

}
